var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);

// PC端获取商品列表 上架商品
router.get('/jkyx/pc/goods_search', function(req, res, next) {
	var param = req.query || req.params; 
	var keyword = param.keyword;
	var status = param.status || '2';
	var page = param.page || 1;
	var page_count = 20;
	var all_page = 0;
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		var sqlStr = 'SELECT * FROM goods WHERE name like "%' + keyword + '%" and status =' + status + ' order by id asc; select * from goods_spec where goods_id in (select goods_id FROM goods_classify_relation) and status = 1';
		connection.query(sqlStr,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var index = (page - 1) * page_count;
				var tempGoods = [];
				// 处理商品 和 规格
				var goods = results[0];
				var goods_len = goods.length;
				var specs = results[1];
				var spec_len = specs.length;
				for (var i = 0; i < goods_len; i ++) {
					var salecount = 0;
					var spec = [];
					var stock = 0;
					for (var j = 0; j < spec_len; j ++) {
						if (specs[j].goods_id == goods[i].id) {
							salecount = salecount + specs[j].salecount;
							stock = stock + specs[j].stock;
							spec.push({
								id: specs[j].id,
								salecount: specs[j].salecount,
								price: specs[j].price / 100,
								spec: specs[j].spec,
								origprice: specs[j].origprice,
								erpcode: specs[j].erpcode,
								stock: specs[j].stock,
							})
						}
					}
					goods[i].salecount = salecount;
					goods[i].stock = stock;
					goods[i].spec = spec;
					goods[i].imgs = JSON.parse(goods[i].imgs);
					if (i >= index && i < (index + page_count)) {
						tempGoods.push(goods[i])
					}
				}
				
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			goods: tempGoods,
	     			total_count: goods.length,
	     		}));	         
			}
		})
	})
});

// PC端获取商品列表 上架商品
router.get('/jkyx/pc/goods_list', function(req, res, next) {
	var param = req.query || req.params; 
	var page = param.page || 1;
	var page_count = 20;
	var all_page = 0;
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_get_goods,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var index = (page - 1) * page_count;
				var tempGoods = [];
				// 处理商品 和 规格
				var goods = results[0];
				var goods_len = goods.length;
				var specs = results[1];
				var spec_len = specs.length;
				for (var i = 0; i < goods_len; i ++) {
					var salecount = 0;
					var spec = [];
					var stock = 0;
					for (var j = 0; j < spec_len; j ++) {
						if (specs[j].goods_id == goods[i].id) {
							salecount = salecount + specs[j].salecount;
							stock = stock + specs[j].stock;
							spec.push({
								id: specs[j].id,
								salecount: specs[j].salecount,
								price: specs[j].price / 100,
								spec: specs[j].spec,
								origprice: specs[j].origprice,
								erpcode: specs[j].erpcode,
								stock: specs[j].stock,
							})
						}
					}
					goods[i].salecount = salecount;
					goods[i].stock = stock;
					goods[i].spec = spec;
					goods[i].imgs = JSON.parse(goods[i].imgs);
					if (i >= index && i < (index + page_count)) {
						tempGoods.push(goods[i])
					}
				}
				
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			goods: tempGoods,
	     			total_count: goods.length
	     		}));	         
			}
		})
	})
});

// PC端获取商品列表 下架商品
router.get('/jkyx/pc/goods_list2', function(req, res, next) {
	var param = req.query || req.params; 
	var page = param.page || 1;
	var page_count = 20;
	var all_page = 0;
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_get_goods2,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var index = (page - 1) * page_count;
				var tempGoods = [];
				// 处理商品 和 规格
				var goods = results[0];
				var goods_len = goods.length;
				var specs = results[1];
				var spec_len = specs.length;
				for (var i = 0; i < goods_len; i ++) {
					var salecount = 0;
					var spec = [];
					var stock = 0;
					for (var j = 0; j < spec_len; j ++) {
						if (specs[j].goods_id == goods[i].id) {
							salecount = salecount + specs[j].salecount;
							stock = stock + specs[j].stock;
							spec.push({
								id: specs[j].id,
								salecount: specs[j].salecount,
								price: specs[j].price / 100,
								spec: specs[j].spec,
								origprice: specs[j].origprice,
								erpcode: specs[j].erpcode,
								stock: specs[j].stock,
							})
						}
					}
					goods[i].salecount = salecount;
					goods[i].stock = stock;
					goods[i].spec = spec;
					goods[i].imgs = JSON.parse(goods[i].imgs);
					if (i >= index && i < (index + page_count)) {
						tempGoods.push(goods[i])
					}
				}
				
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			goods: tempGoods,
	     			total_count: goods.length
	     		}));	         
			}
		})
	})
});
// PC端获取商品列表 删除商品
router.get('/jkyx/pc/goods_list3', function(req, res, next) {
	var param = req.query || req.params; 
	var page = param.page || 1;
	var page_count = 20;
	var all_page = 0;
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_get_goods3,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var index = (page - 1) * page_count;
				var tempGoods = [];
				// 处理商品 和 规格
				var goods = results[0];
				var goods_len = goods.length;
				var specs = results[1];
				var spec_len = specs.length;
				for (var i = 0; i < goods_len; i ++) {
					var salecount = 0;
					var spec = [];
					var stock = 0;
					for (var j = 0; j < spec_len; j ++) {
						if (specs[j].goods_id == goods[i].id) {
							salecount = salecount + specs[j].salecount;
							stock = stock + specs[j].stock;
							spec.push({
								id: specs[j].id,
								salecount: specs[j].salecount,
								price: specs[j].price / 100,
								spec: specs[j].spec,
								origprice: specs[j].origprice,
								erpcode: specs[j].erpcode,
								stock: specs[j].stock,
							})
						}
					}
					goods[i].salecount = salecount;
					goods[i].stock = stock;
					goods[i].spec = spec;
					goods[i].imgs = JSON.parse(goods[i].imgs);
					if (i >= index && i < (index + page_count)) {
						tempGoods.push(goods[i])
					}
				}
				
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			goods: tempGoods,
	     			total_count: goods.length
	     		}));	         
			}
		})
	})
});

// PC端获取商品详情
router.get('/jkyx/pc/goods_detail', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || '';
	if (goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入商品ID',
 			status:'104',
 		}));
 		return;
	} 
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_goods_detail,[goods_id,goods_id,goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				
				var goods = results[0][0];
				goods.imgs = JSON.parse(goods.imgs);
				goods.pre_sale_time = CommonJS.formatDate(goods.pre_sale_time);
				var spec = results[1];
				var classify = results[2];
				
				var len = spec.length;
				for (var i = 0; i < len; i ++) {
					spec[i].price = spec[i].price / 100;
					spec[i].origprice = spec[i].origprice / 100;
				}
				
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			goods: goods,
	     			spec: spec,
	     			classify: classify
	     		}));	         
			}
		})
	})
});

// PC端 保存商品信息
router.get('/jkyx/pc/save_goods', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || '';
	var name = param.name || '';
	var descr = param.descr || '';
	var imgs = param.imgs || [];
	var detail_imgs = param.detail_imgs || '';
	var pre_sale_time = param.pre_sale_time || '';
	pre_sale_time = CommonJS.formatToTimestamp(pre_sale_time);
	var create_time = param.create_time || '';
	if (goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入商品ID',
 			status:'104',
 		}));
 		return;
	} 
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_save_goods,[name,descr,pre_sale_time,create_time,imgs,detail_imgs,goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});
// PC端 商品下架
router.get('/jkyx/pc/xia_goods', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || '';
	if (goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入商品ID',
 			status:'104',
 		}));
 		return;
	} 
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_xia_goods,[goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});
// PC端 商品删除
router.get('/jkyx/pc/delete_goods', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || '';
	if (goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入商品ID',
 			status:'104',
 		}));
 		return;
	} 
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_delete_goods,[goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});
// PC端 商品上架
router.get('/jkyx/pc/shang_goods', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || '';
	if (goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入商品ID',
 			status:'104',
 		}));
 		return;
	} 
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_shang_goods,[goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});

// PC端 新增商品信息
router.get('/jkyx/pc/add_goods', function(req, res, next) {
	var param = req.query || req.params; 
	var name = param.name || '';
	var descr = param.descr || '';
	var imgs = param.imgs || [];
	var detail_imgs = param.detail_imgs || '';
	var pre_sale_time = param.pre_sale_time || '';
	pre_sale_time = CommonJS.formatToTimestamp(pre_sale_time);
	var create_time = param.create_time || '';
	
	// 处理主图
	imgs = JSON.parse(imgs);
	var imgs_len = imgs.length;
	var tempImgs = [];
	for (var i = 0; i < imgs_len; i ++) {
		tempImgs.push(imgs[i].url);
	}
	tempImgs = JSON.stringify(tempImgs) || '';
	// 处理详情图
	detail_imgs = JSON.parse(detail_imgs);
	var detail_imgs_len = detail_imgs.length;
	var tempImgs2 = [];
	for (var i = 0; i < detail_imgs_len; i ++) {
		tempImgs2.push(detail_imgs[i].url);
	}
	detail_imgs = tempImgs2[0] || '';
	
	
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_add_goods,[name,descr,tempImgs,detail_imgs,pre_sale_time,create_time],function (err, results) {
//			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var goods_id = results[1][0].id;
				var sql = 'update goods set goods_id = ? where id = ?';
				connection.query(sql,[goods_id,goods_id],function (err, results) {
					connection.release();
					if (err){
					  	res.end(JSON.stringify({
			     			msg: '数据库查询失败',
			     			status:'103',
			     			err: err
			     		}));
					}else{
						res.end(JSON.stringify({
			     			msg: '操作成功',
			     			status:'100',
			     			goods_id: goods_id
			     		}));
					}
				})     
			}
		})
	})
});

// PC端 新增商品分类
router.get('/jkyx/pc/add_goods2', function(req, res, next) {
	var param = req.query || req.params; 
	var goods_id = param.goods_id || '';
	// 分类id
	var classify = param.classify;
	if (goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入商品ID',
 			status:'104',
 		}));
 		return;
	} 
	if (classify == '') {
		res.end(JSON.stringify({
 			msg: '操作成功',
 			status:'100',
 		}));
 		return;
	} 
	classify = classify.split(',');
	var len = classify.length;
	console.log(len);
	var value2 = '';
	var timer = CommonJS.getNowFormatDate();
	for (var i = 0; i < len; i++) {
		var classify_id = classify[i];
		var values = "'" + goods_id + "'" + ',' 
  			 + "'" + classify_id + "'" + ',' 
  			 + "'" + timer + "'" + ',' 
  			  + "'" + timer + "'";
        if (i == (len - 1)) {
        		values = '(' + values + ')';
        }else{
        		values = '(' + values + '),';
        }
        value2 = value2 + values;
	}
	var inserSql = 'insert into goods_classify_relation (goods_id,classify_id,create_time,update_time) values ' + value2 + ';';
	
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(inserSql,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));
			}
		})
	})
});


module.exports = router;
